/*****************************************************
*
*               COPYRIGHT (c) 2017-2019 Hargic Corporation
*                         All Rights Reserved
*
* The source code contained or described herein and all documents
* related to the source code ("Material") are owned by Hargic
* Corporation or its licensors.  Title to the Material remains
* with Hargic Corporation or its suppliers and licensors.
*
* The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used,
* copied, reproduced, modified, published, uploaded, posted, transmitted,
* distributed, or disclosed in any way except in accordance with the
* applicable license agreement.
*
* No license under any patent, copyright, trade secret or other
* intellectual property right is granted to or conferred upon you by
* disclosure or delivery of the Materials, either expressly, by
* implication, inducement, estoppel, except in accordance with the
* applicable license agreement.
*
* Unless otherwise agreed by Hargic in writing, you may not remove or
* alter this notice or any other notice embedded in Materials by Hargic
* or Hargic's suppliers or licensors in any way.
*
** Author: shengda.huang
** Date: 2019-03-19
*
*****************************************************/

/***** Include files ********************************/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>

/***** Defines **************************************/
#define BEEP_PATH   "/dev/led_control_demo"

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
int main(int argc, char **argv)
{
    int i, on, cnt, ret;
    char *val = NULL;
    unsigned char value;

    int fd = open(BEEP_PATH, O_RDWR);
    if (fd < 0)
    {
        printf("%s: open %s failed fd %d\n", __func__, BEEP_PATH, fd);
        return -1;
    }

    for (i = 0, cnt = 0; i < 100; i++)
    {
        ret = read(fd, &on, 1);
        printf("%s: read ret %d on %d\n", __func__, ret, on);

        on = i & 0x01;

#if 1
        val = on ? "on" : "off";
        // value = (unsigned char)on;
        ret = write(fd, val, strlen(val));
        if(ret < 0)
            printf("%s: write %s failed ret %d\n", __func__, val, ret);
#else
        ioctl(fd, on, 0);
#endif

        printf("%s: beep %s %d cnt %d\n", __func__, on ? "on" : "off", on, ++cnt);
        usleep(500 * 1000);
    }

    printf("%s: exit\n", __func__);
    close(fd);
    return 0;
}

/* End of file */



